<?php
/*
	提现
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
cfc('core')->checklogin();
$credit_types=credit_types(true);
if($_RQ['op']=='get'){
	$up_type=$tixianset['up_type']?:3;
	if($_SESSION['onemerchid']){
		$member=model('jy_merch/member')->getdetail($_SESSION['onemerchid'],$_SESSION['uid']);
	}else{
		$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
	}
	$member2=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
	$member=array_merge($member2,$member);
	$tinum=intval($return['allmoney']/$tixianset['man_fee']);
	$return['man_fee']=$tixianset['man_fee'];
	$return['member']=$member;
	$return['params']['type']=$_RQ['type']?:5;
	$return['params']['feetype']=0;
	$return['allmoney']=$member['credit'.$return['params']['type']];
	$return['typetitle']='能量值';
	$return['tips']=$tips;
	$return['is_open']=$tixianset['is_open'];
	$return['open_upbaofee']=$tixianset['open_upbaofee'];
	$data['balance_per']=$this->setting['balance_per2']?:0;
	$return['data']=$data;
	if($this->setting['withdrawtype2']){
		$withdrawtype=array();
		$wxapppay_types=array();
		$check=strstr($this->setting['withdrawtype2'],'0');
		if($check){
			$withdrawtype['0']=$this->pay_types['wechat'];
			$pay_type=$pay_type?:'0';
		}
		$check=strstr($this->setting['withdrawtype2'],'1');
		if($check){
			$withdrawtype['1']=$this->pay_types['alipay'];
			$pay_type=$pay_type?:'1';
		}
		$check=strstr($this->setting['withdrawtype2'],'2');
		if($check){
			$withdrawtype['2']=$this->pay_types['union'];
			$pay_type=$pay_type?:'2';
		}
		$check=strstr($this->setting['withdrawtype2'],'3');
		if($check){
			$withdrawtype['3']=$this->pay_types['credit'];
			$pay_type=$pay_type?:'3';
		}
		$return['pay_types']=$withdrawtype;
	}else{
		$return['pay_types']=array('提现到微信钱包','提现到支付宝','提现到银行卡','提现余额');
		$pay_type='0';
	}
	$wxapppay_types=array();
	foreach($return['pay_types'] as $k=>$pv){
		$wxapppay_types[]=array(
			'key'=>$k,
			'title'=>$pv,
		);
	}
	$return['wxapppay_types']=$wxapppay_types;
	$check=strstr($this->setting['withdrawtype2'],'1');
	if($check){
		$type_array[1]='提现到支付宝';
        $post1=$this->get('balance',array('pay_type'=>'alipay','mid'=>$_SESSION['uid']),array('alipay','realname'),'id desc');
	}
	$check=strstr($this->setting['withdrawtype2'],'2');
	if($check){
		$type_array[2]='提现到银行卡';
        $post2=$this->get('balance',array('pay_type'=>'union','mid'=>$_SESSION['uid']),array('bankname','bankcard','realname'),'id desc');
        $post2['bankcard1']=$post2['bankcard'];
	}
    if($post1 || $post2) {
        $return['post'] = array_merge($post1, $post2);
    }
	$return['post']['type']=$pay_type;
	set_html('title','我的'.$return['typetitle']);
	exi($return);
}
if($_RQ['op']=='tixian'){
	$post=get_postdata();
	if($post['type']=='no'){
		exi('请选择提现方式！','erroe');
	}
	if(!$post['fee']){
		exi('请输入提现额度','error');
	}
	if($post['fee']<$this->setting['balance_fee2'] && $this->setting['balance_fee2']){
		exi('提现额度不能小于'.$this->setting['balance_fee2'],'error');
	}
	$fee=$post['fee'];
	if(is_numeric($fee)){
	}else{
		exi('提现额度必须是数字！','error');
	}
	$money=$fee;
	$service_money=$this->setting['balance_per2']*$money/100;
	if($post['type']==1){
		if(!$post['realname']){
			exi('请输入姓名','error');
		}
		if(!$post['alipay']){
			exi('请输入支付宝帐号','error');
		}
		if(!$post['alipay1']){
			exi('请确认帐号','error');
		}
		if($post['alipay1']!=$post['alipay']){
			exi('确认帐号与支付宝帐号不匹配！','error');
		}
	}
	if($post['type']==2){
		if(!$post['realname']){
			exi('请输入姓名','error');
		}
		if(!$post['bankname']){
			exi('请输入所属银行','error');
		}
		if(!$post['bankcard']){
			exi('请银行卡号','error');
		}
		if(!$post['bankcard1']){
			exi('请确认卡号','error');
		}
		if($post['bankcard']!=$post['bankcard1']){
			exi('确认卡号与银行卡号不匹配！','error');
		}
	}
	if($post['feetype']==1){
		$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
		if($member['nengfee']<$fee){
			exi('你当前《能量增值》不足！无法提现！','error');
		}
		if($post['type']==0){
			$post['pay_type']='wechat';
		}elseif($post['type']==1){
			$post['pay_type']='alipay';
		}elseif($post['type']==2){
			$post['pay_type']='union';
		}elseif($post['type']==3){
			$post['pay_type']='credit';
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$_SESSION['onemerchid'],
			'mid'=>$_SESSION['uid'],
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'money'=>$money,
			'nengfee'=>$fee,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'createtime'=>TIMESTAMP,
			'fee_type'=>5,
			'status'=>0,
		);
		$new_balance['balance_pay']=$service_money;
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$log=array(
				'merchid'=>$_SESSION['onemerchid'],
				'mid'=>$_SESSION['uid'],
				'fee'=>$fee,
				'is_add'=>0,
		);
		$this->model('task')->add_nlog($log,1);
		$this->update('member',array('nengfee -='=>$fee),array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
		exi('提现成功！','success','goback');
	}else{
		model('core/member/cash')->check_cash_xunibi($_SESSION['uid'],$fee,$money,5,$_SESSION['onemerchid']);
		$cash=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$_SESSION['onemerchid'],
			'mid'=>$_SESSION['uid'],
			'ordersn'=>get_ordersn('core_member_cash','ordersn','CASH'),
			'money'=>$money,
			'type'=>intval($post['type']),
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'service_money'=>$service_money,
			'remark'=>'能量值提现',
			'credit_type'=>5,
			'credit_fee'=>$fee,
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		$cash['cash_money']=$cash['money']-$cash['service_money'];
		pdo_insert('core_member_cash',$cash);
		$id=pdo_insertid();
		$type_array=array('能量值提现到微信钱包','能量值提现到支付宝','能量值提现到银行卡');
		$typetitle=$type_array[$cash['type']];
		model('core/member/cash')->notice_tixian($_SESSION['uid'],$money,$typetitle);
		if(model('core/member/cash')->setting['check'] || $params['to_type'] !=0){
			exi('申请提现成功，请等待管理员审核！','','goback');
		}else{
			model('core/member/cash')->pay($id);
			exi('提现成功！','success','goback');
		}
	}
}
if($_RQ['op']=='list'){	
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	$nlog_type=$this->nlog_type;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_merch3fee_nlog',$params,array($pindex,$psize),$total,'','','id desc');
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['is_addtitle']=$item['is_add']?'获得':'消费';
			$item['typetitle']=$nlog_type[$item['type']];
		}
	}
	$return['list']=$list;
	exi($return);
}